<!-- Posts.vue -->
<template>
  <div class="posts-management">
    <div class="card">
      <div class="card-header">
        <h2>我的帖子</h2>
        <button class="btn-refresh" @click="fetchPosts">
          <i class="icon-refresh"></i> 刷新
        </button>
      </div>

      <div class="table-container">
        <table>
          <thead>
          <tr>
            <th>标题</th>
            <th>发布时间</th>
            <th>浏览数</th>
            <th>点赞数</th>
            <th>操作</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="post in posts" :key="post.id">
            <td>{{ post.title }}</td>
            <td>{{ $formatDate(post.createTime) }}</td>
            <td>{{ post.viewCount }}</td>
            <td>{{ post.likeCount }}</td>
            <td>
              <button class="btn-edit" @click="handleEdit(post)">
                <i class="icon-edit"></i> 编辑
              </button>
              <button class="btn-delete" @click="handleDelete(post)">
                <i class="icon-trash"></i> 删除
              </button>
            </td>
          </tr>
          </tbody>
        </table>
      </div>

      <div class="pagination">
        <button @click="prevPage" :disabled="currentPage === 1">
          <i class="icon-chevron-left"></i>
        </button>
        <span>第 {{ currentPage }} 页</span>
        <button @click="nextPage" :disabled="posts.length < pageSize">
          <i class="icon-chevron-right"></i>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Posts',
  data() {
    return {
      user:null,
      posts: [],
      currentPage: 1,
      pageSize: 10,
      loading: false
    }
  },
  created() {
    this.fetchPosts();
  },
  methods: {
    async fetchPosts() {
      // 从localStorage获取当前用户ID
      const storedUser = localStorage.getItem('user');
      this.user = storedUser ? JSON.parse(storedUser) : null;
      const uid = storedUser ? JSON.parse(storedUser).uid : null;

      try {
        const response = await this.$axios.get(`/api/percenter/posts`, {
          params: { uid }
        });
        console.log('我在这里哦!'+uid);
        console.log(this.posts);
        // 检查响应结构并更新用户数据
        if (response.data && response.data.data) {
          this.posts = response.data.data;

          console.log(response.data);
        }
      } catch (error) {
        this.$message.error('获取帖子失败');
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--;
        this.fetchPosts();
      }
    },
    nextPage() {
      this.currentPage++;
      this.fetchPosts();
    }
  }
}
</script>

<style scoped>
.posts-management {
  padding: 20px;
}

.btn-refresh {
  padding: 8px 16px;
  background-color: #1976d2;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

.btn-refresh i {
  margin-right: 5px;
}

.table-container {
  overflow-x: auto;
  margin: 20px 0;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #e0e0e0;
}

th {
  background-color: #f5f7fa;
  color: #607d8b;
  font-weight: 500;
}

.btn-edit, .btn-delete {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 5px;
  display: inline-flex;
  align-items: center;
}

.btn-edit {
  background-color: #ffc107;
  color: white;
}

.btn-delete {
  background-color: #f44336;
  color: white;
}

.btn-edit i, .btn-delete i {
  margin-right: 5px;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  padding: 20px;
}

.pagination button {
  width: 32px;
  height: 32px;
  border: 1px solid #e0e0e0;
  background-color: white;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pagination span {
  color: #607d8b;
}
</style>